﻿'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.

'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details.

'You should have received a copy of the GNU General Public License
'along with this program.  If not, see <http://www.gnu.org/licenses/>.

Imports System.IO
Public Class frmMain
    Dim dropdown As String = "none"
    Public errorchecks As Boolean = True
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ffile As New FileInfo("wow.exe")
        If ffile.Exists = False Then
            MsgBox("Error 1001: wow.exe not found.  Please make sure this program is in your World of Warcraft directory.")
            Me.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnVote.Click
        ' Starts the page in the users default web browser.
        System.Diagnostics.Process.Start("http://www.wowgasm.org/tools/voter.php")
        bttnVote.Enabled = False
        bttnVote.Text = "Thank you!"
    End Sub

    Private Sub bttnRetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Open_Retail()
    End Sub

    Function clear_cache() As Boolean
        ' I am giving the user an option to clear or not because while on retail
        ' I have noticed lag after clearing my cache when viewing items, quests, etc.
        ' that were linked in chat. The cache was created to prevent that.
        Try
            If chckCache.Checked = True Then
                If IO.Directory.Exists("Cache") = True Then
                    IO.Directory.Delete("Cache", True)
                End If
            End If
        Catch
            MsgBox("Error 1004: Could not clear your cache.  This could be caused by the launcher being opened twice, WoW being open, or the Vista UAC.")
        End Try
    End Function

    Private Sub bttnPrvSvr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPrvSvr.Click
        If dropdown = "none" Then
            MsgBox("Error 1003: No game selected")
        End If
        If dropdown = "wowgasm" Then
            Open_Gasm()
        End If
        If dropdown = "retail" Then
            Open_Retail()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Starts the page in the users default web browser.
        System.Diagnostics.Process.Start("http://www.wowgasm.org/en/agreement.php")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ' Starts the page in the users default web browser.
        System.Diagnostics.Process.Start("http://www.wowgasm.org/en/index.php")
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub

    Function Open_Retail() As Boolean
        ' I am calling a separate function because my clear cache function used to be
        ' a lot more involved.
        clear_cache()

        ' To tell if the user has the US or EU client, I list every directory in the Data folder.
        ' If the folder enUS exists, then the user has enUS, if the folder enEU exists, then they have enEU.
        ' Using LCase just because I do not have access to a WoW installation at the moment.
        Dim locale As String = Find_Locale()
        If locale = "none" Then
            Exit Function
        End If
        Dim realmlist As String = "Data/realmlist.wtf"
        If File_Exists("./", "realmlist.wtf") = True Then
            realmlist = "realmlist.wtf"
        End If
        If File_Exists("./Data/en" & UCase(locale), "realmlist.wtf") Then
            realmlist = "Data/en" & UCase(locale) & "/realmlist.wtf"
        End If
        Dim fs As New FileStream(realmlist, FileMode.Create, FileAccess.Write)
        Dim writer As New StreamWriter(fs)
        writer.WriteLine("set realmlist " & locale & ".logon.worldofwarcraft.com")
        writer.WriteLine("set patchlist " & locale & ".update.worldofwarcraft.com")
        writer.Close()
        Try
            System.Diagnostics.Process.Start("wow.exe")
        Catch
            MsgBox("Error: 1002: Could not open wow.exe.  Could the Vista UAC be preventing it?")
            Exit Function
        End Try
        Me.Close()
    End Function

    Function Open_Gasm() As Boolean
        clear_cache()
        Dim locale As String = Find_Locale()
        If locale = "none" Then
            Exit Function
        End If
        Dim realmlist As String = "Data/realmlist.wtf"
        If File_Exists("./", "realmlist.wtf") = True Then
            realmlist = "realmlist.wtf"
        End If
        If File_Exists("./Data/en" & UCase(locale), "realmlist.wtf") Then
            realmlist = "Data/en" & UCase(locale) & "/realmlist.wtf"
        End If
        Dim fs As New FileStream(realmlist, FileMode.Create, FileAccess.Write)
        Dim writer As New StreamWriter(fs)
        writer.WriteLine("set realmlist logon.wowgasm.org")
        writer.Close()
        Try
            System.Diagnostics.Process.Start("wow.exe")
        Catch
            MsgBox("Error: 1002: Could not open wow.exe.  Could the Vista UAC be preventing it?")
            Exit Function
        End Try
        Me.Close()
    End Function

    Function Find_Locale() As String
        Dim Root As New DirectoryInfo("Data")
        Dim Dirs As DirectoryInfo() = Root.GetDirectories("*.*")
        Dim DirectoryName As DirectoryInfo
        Dim found As Boolean = False
        Dim locale As String = "none"
        For Each DirectoryName In Dirs
            If LCase(DirectoryName.Name) = "enus" Then
                locale = "us"
                found = True
            End If
            If LCase(DirectoryName.Name) = "eneu" Then
                locale = "eu"
                found = True
            End If
            If LCase(DirectoryName.Name) = "engb" Then
                locale = "gb"
                found = True
            End If
        Next
        ' Error protection
        If found = False Or locale = "none" Then
            MsgBox("Error 1000: Could not determine the locale of your client.  This launcher only supports enUS and enEU clients.")
        End If
        Return locale
    End Function

    Function File_Exists(ByVal Root_Directory As String, ByVal Search_File As String) As Boolean
        Dim exists As Boolean = False
        Dim Root As New DirectoryInfo(Root_Directory)
        Dim Files As FileInfo() = Root.GetFiles("*.*")
        Dim FileName As FileInfo
        For Each FileName In Files
            If FileName.Name = Search_File Then
                exists = True
            End If
        Next
        Return exists
    End Function

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
        System.Diagnostics.Process.Start("http://code.google.com/p/wowgasmlauncher/")
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Starts the page in the users default web browser.
        System.Diagnostics.Process.Start("http://wowgasm.org/index.php")
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedIndex = 0 Then
            dropdown = "wowgasm"
        End If
        If ComboBox1.SelectedIndex = 1 Then
            dropdown = "retail"
        End If
    End Sub
End Class
